home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17640 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  70 lines

  1. Path: news.dal.ca!news
  2. From: Klaus.Eichele@Dal.Ca (Klaus Eichele)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: BC 3.1 - 4.5 (error!?)
  5. Date: Wed, 17 Apr 1996 01:53:05 GMT
  6. Organization: Dalhousie University
  7. Message-ID: <4l14s3$lg1@News.Dal.Ca>
  8. References: <3173D628.48F6@demos.su>
  9. NNTP-Posting-Host: rewasylishen.chem.dal.ca
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. Alexey Ruzin <00alex@demos.su> wrote:
  13.  
  14. >Hi, All
  15.  
  16. >Here is simple example of abnormal programming.
  17. >But BC crashed (sorry, it works and even compiles
  18. >the program, but it is unpredictable what will occur)
  19. >on it:
  20.  
  21. >#include <stdio.h>
  22.  
  23. >int st_func( int a )
  24. >{
  25. >  static int p=a;
  26. >  return p++;
  27. >}
  28.  
  29. >int main( int argc, char *argv[] )
  30. >{
  31. >  int i;
  32. >  if( argc != 1 )
  33. >  {
  34. >    for( i = 10; i>0; i-- )
  35. >      printf( "%d\n", st_func( i ) );
  36. >  }
  37. >  else
  38. >  {
  39. >    for( i = 0; i<10; i++ )
  40. >      printf( "%d\n", st_func( i ) );
  41. >  }
  42. >  return 0;
  43. >}
  44.  
  45. >Enjoy...
  46.  
  47. >If you know what is wrong reply please :)
  48.  
  49. Hi, 
  50. I am not sure what this program is supposed to achieve, but compiling
  51. it sure produces an error: illegal initialization.
  52. >  static int p=a;
  53. The reason is that at initialization time a is (or would be)
  54. undefined. If you want your function to work, you will need to change
  55. it to:
  56.  
  57. int st_func( int a )
  58. {
  59.   static int p;
  60.   p=a;
  61.   return p++;
  62. }
  63.  
  64. Good Luck,
  65. Klaus
  66. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  67. Klaus Eichele         keichele@is.dal.ca  
  68. http://is.dal.ca/~keichele/keichele.html
  69.  
  70.